package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.operation.domain.EatDO;
import com.zh.operation.domain.PaymentDO;
import com.zh.operation.service.EatService;
import com.zh.operation.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Api("用餐【每日食谱】")
public class EatController extends BaseController {
	@Autowired
	private EatService eatService;
	@Autowired
	private PaymentService paymentService;



	@Log("当天食谱")
	@ResponseBody
	@RequestMapping(value = "listOperationEat", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationEat(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<EatDO> eatList = eatService.list(query);
		for (EatDO eat:eatList){
			List<String> paymetName = eatService.getPaymetName(eat.getId());
			if(paymetName!=null){
				eat.setPaymentIds(paymetName);
			}
		}
		int total = eatService.count(query);
		PageUtils pageUtils = new PageUtils(eatList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑当天食谱")
	@ResponseBody
	@RequestMapping(value = "editOperationEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationEatById(@RequestBody  EatDO eat){
		if(eat==null || CheckEmptyUtil.isEmpty(eat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		EatDO eatDO = eatService.get(eat.getId());
		List<String> paymetName = eatService.getPaymetName(eat.getId());
		eatDO.setPaymentName(paymetName.toString());
	    return ResultVO.success(eatDO);
	}

	@Log("新增当天食谱")
	@ResponseBody
	@RequestMapping(value = "saveOperationEat", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationEat(@RequestBody  EatDO eat){
		if(eat==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		eat.setCreateBy(getUserId());
		if(eatService.save(eat)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("更新当天食谱")
	@ResponseBody
	@RequestMapping(value = "updateOperationEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationEatById(@RequestBody EatDO eat){
		if(eat==null || CheckEmptyUtil.isEmpty(eat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		eatService.update(eat);
		return ResultVO.success();
	}

	@Log("删除当天食谱")
	@ResponseBody
	@RequestMapping(value = "removeOperationEatById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationEatById(@RequestBody EatDO eat){
		if(eat==null || CheckEmptyUtil.isEmpty(eat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(eatService.remove(eat.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}

    @RequestMapping(value = "batchRemoveOperationEatByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除食谱信息")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
    	if(jsonParam==null){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	List<String> ids= (List<String>)jsonParam.get("ids");
    	if(CheckEmptyUtil.isEmpty(ids)){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	String[] strs1=ids.toArray(new String[ids.size()]);
    	if(eatService.batchRemove(strs1)>0){
    		return ResultVO.success();
    	}
    	return ResultVO.error();
    }

	@Log("获取食谱菜单和套餐关联表的paymetIds")
	@ResponseBody
	@RequestMapping(value = "getOperationPamenId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO getOperationPamenId(@RequestBody EatDO eat){
		if(eat==null || CheckEmptyUtil.isEmpty(eat.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> paymetIds = eatService.getPaymetIds(eat.getId());
		return ResultVO.success(paymetIds);
	}


//	@Log("获取套餐列表")
//	@ResponseBody
//	@RequestMapping(value = "getOperationPaymentList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//	public ResultVO getOperationPaymentList(@RequestBody Map<String, Object> params){
//		if(CheckEmptyUtil.isEmpty(params)){
//			return ResultVO.error(ResultEnum.PARAME_ERROR);
//		}
//		//查询列表数据
//		Query query = new Query(params);
//		List<PaymentDO> paymentList = paymentService.list(query);
//		int total = paymentService.count(query);
//		PageUtils pageUtils = new PageUtils(paymentList, total);
//		return ResultVO.success(pageUtils);
//	}

}
